package com.hanbang.maige.view.home.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.hanbang.maige.R;
import com.hanbang.maige.code.ActivityManager;
import com.hanbang.maige.code.base.view.activity.BaseActivity;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.utils.ui.DrawableUtils;
import com.hanbang.maige.utils.ui.SnackbarUtil;
import com.hanbang.maige.view.aboutme.fragment.AboutMeBaseFragment;
import com.hanbang.maige.view.home.fragment.HomeFragment;
import com.hanbang.maige.view.huodong.fragment.HuoDongFragment;
import com.hanbang.maige.view.quanzi.fragment.QuanZiFragment;

import butterknife.BindView;

/**
 * 作者　　: 李坤
 * 创建时间:2016/9/19　17:04
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */
public class HomeActivity extends BaseActivity implements AHBottomNavigation.OnTabSelectedListener {
    @BindView(R.id.bottom_navigation_bar)
    AHBottomNavigation ahBottomNavigation;
    private long exitTime = 0;
    private static String broadFlag = "Buttom_BroadFlag";

    FragmentManager fragmentManager;

    HomeFragment homeFragment;
    HuoDongFragment huoDongFragment;
    QuanZiFragment quanZiFragment;
    AboutMeBaseFragment aboutMeFragment;
    @BindView(R.id.content)
    FrameLayout content;

    public static void startUI(Activity activity) {
        Intent intent = new Intent(activity, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivity(intent);
    }

    @Override
    public int getStatusBarPaddingTop() {
        return 0;
    }

    @Override
    public int getStatusBarResource() {
        return R.color.translucent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                homeFragment = null;
                huoDongFragment = null;
                quanZiFragment = null;
                aboutMeFragment = null;
                HomeActivity.this.finish();
            }
        }, new IntentFilter(BroadcastUtils.EXIT_LOGIN));
    }

    @Override
    public int getContentView() {
        return R.layout.activity_home;
    }


    @Override
    public void initView() {


        ahBottomNavigation.setAccentColor(getResources().getColor(R.color.main_color));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_1),
                DrawableUtils.getStateSelectDrawable(R.drawable.material_home_select, R.drawable.material_home_no)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_2),
                DrawableUtils.getStateSelectDrawable(R.drawable.material_huodong_select, R.drawable.material_huodong_no)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_3),
                DrawableUtils.getStateSelectDrawable(R.drawable.material_quanzi_select, R.drawable.material_quanzi_no)));
        ahBottomNavigation.addItem(new AHBottomNavigationItem(getResources().getString(R.string.bottom_4),
                DrawableUtils.getStateSelectDrawable(R.drawable.material_about_me_select, R.drawable.material_aboout_me_no)));

        ahBottomNavigation.setDefaultBackgroundColor(getResources().getColor(R.color.white));
        ahBottomNavigation.setForceTitlesDisplay(true);
        ahBottomNavigation.setOnTabSelectedListener(this);


        fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        homeFragment = new HomeFragment();
        ft.add(R.id.content, homeFragment);
        ft.commit();


    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public boolean onTabSelected(int position, boolean wasSelected) {
        if (wasSelected) return false;
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (position == 0) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                ft.add(R.id.content, homeFragment);
            } else {
                hintAllFragment(ft);
                ft.show(homeFragment);
            }
            ft.commit();
        } else if (position == 1) {
            if (huoDongFragment == null) {
                huoDongFragment = new HuoDongFragment();
                ft.add(R.id.content, huoDongFragment);
            } else {
                hintAllFragment(ft);
                ft.show(huoDongFragment);
            }
            ft.commit();
        } else if (position == 2) {
            if (quanZiFragment == null) {
                quanZiFragment = new QuanZiFragment();
                ft.add(R.id.content, quanZiFragment);
            } else {
                hintAllFragment(ft);
                ft.show(quanZiFragment);
            }
            ft.commit();
        } else if (position == 3) {
            if (aboutMeFragment == null) {
                aboutMeFragment = new AboutMeBaseFragment();
                ft.add(R.id.content, aboutMeFragment);
            } else {
                hintAllFragment(ft);
                ft.show(aboutMeFragment);
            }
            ft.commit();
        }
        return true;
    }

    public void setCurrentItem(int postion) {
        ahBottomNavigation.setCurrentItem(postion);
    }

    private void hintAllFragment(FragmentTransaction ft) {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (huoDongFragment != null) {
            ft.hide(huoDongFragment);
        }
        if (quanZiFragment != null) {
            ft.hide(quanZiFragment);
        }
        if (aboutMeFragment != null) {
            ft.hide(aboutMeFragment);
        }

    }


    @Override
    public void onBackPressed() {
        isExitApplication(this);
    }


    public void isExitApplication(Context context) {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Snackbar snackbar = SnackbarUtil.showShort(this, "再按一次退出程序", SnackbarUtil.Info);
            snackbar.setActionTextColor(Color.WHITE);
            snackbar.setAction(R.string.snackbar_action, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    exitTime = 0;
                }
            });
            snackbar.show();
            exitTime = System.currentTimeMillis();
        } else {
            // 退出
            ActivityManager.getInstance().exitAllActivity();
        }
    }
}
